371a22
@@ -19,20 +19,26 @@
package org.apache.camel.spring.boot;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.TypeConversionException;
 import org.apache.camel.support.TypeConverterSupport;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.convert.ConversionFailedException;
 import org.springframework.core.convert.ConversionService;
+import org.springframework.core.convert.ConverterNotFoundException;
+import org.springframework.core.convert.TypeDescriptor;
 
 public class SpringTypeConverter extends TypeConverterSupport {
 
     private final List<ConversionService> conversionServices;
+    private final ConcurrentHashMap<Class<?>, TypeDescriptor> types;
 
     @Autowired
     public SpringTypeConverter(List<ConversionService> conversionServices) {
         this.conversionServices = conversionServices;
+        this.types = new ConcurrentHashMap<>();
     }
 
     @Override
@@ -47,12 +53,33 @@
public class SpringTypeConverter extends TypeConverterSupport {
             return null;
         }
 
+        TypeDescriptor sourceType = types.computeIfAbsent(value.getClass(), TypeDescriptor::valueOf);
+        TypeDescriptor targetType = types.computeIfAbsent(type, TypeDescriptor::valueOf);
+
         for (ConversionService conversionService : conversionServices) {
-            if (conversionService.canConvert(value.getClass(), type)) {
-                return conversionService.convert(value, type);
+            if (conversionService.canConvert(sourceType, targetType)) {
+                try {
+                    return (T)conversionService.convert(value, sourceType, targetType);
+                } catch (ConversionFailedException e) {
+                    // if value is a collection or an array the check ConversionService::canConvert
+                    // may return true but then the conversion of specific objects may fail
+                    //
+                    // https://issues.apache.org/jira/browse/CAMEL-10548
+                    // https://jira.spring.io/browse/SPR-14971
+                    //
+                    if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) {
+                        return null;
+                    } else {
+                        throw new TypeConversionException(value, type, e);
+                    }
+                }
             }
         }
+
         return null;
     }
 
+    private boolean isArrayOrCollection(Object value) {
+        return value instanceof Collection || value.getClass().isArray();
+    }
 }
